/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-5-22
 */
package com.autotag;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import fanglu.util.FileDealer;
import fanglu.util.StringDealer;

import sei.pku.wsdl_analyzer.Endpoint;
import sei.pku.wsdl_analyzer.WSDLFile;
import sei.pku.wsdl_analyzer.WSDLFileAnalyzer;

public class OnwerTagGenerator {
	public static void main(String args[]){
		//part 1, generate owner tag
		File dirFile = new File("C:\\Users\\ftc\\Desktop\\ICSOC\\WSDL_FILE\\WSDL_FILE");
		File tagFile = new File("C:\\Users\\ftc\\Desktop\\ICSOC\\WSDL_FILE\\result\\owner_tag.txt");
//		generateOwnerTag(dirFile, tagFile);
		
		//part 2, stat owner
		statOnwerTag(tagFile);
	}
	
	public static void statOnwerTag(File tagFile){
		try {
			HashMap<String, ArrayList<String>> hostMapUUIDList = new HashMap<String, ArrayList<String>>(); 
			String fileContent = FileDealer.getFileContent(tagFile.getAbsolutePath());
			StringReader sr = new StringReader(fileContent);
			BufferedReader br = new BufferedReader(sr);
			String oneLine = br.readLine();
			String thisUUID = "";
			while (oneLine != null){
				oneLine = oneLine.trim();
				if (oneLine.startsWith("#")){
					thisUUID = new String(oneLine.substring(1));
				}
				else{
					ArrayList<String> thisList = hostMapUUIDList.get(oneLine);
					if (thisList == null){
						thisList = new ArrayList<String>();
						thisList.add(thisUUID);
						hostMapUUIDList.put(oneLine, thisList);
					}else{
						thisList.add(thisUUID);
					}
				}
				oneLine = br.readLine();
			}
			
			br.close();
			sr.close();
			
			System.out.println("---");
			Iterator<String> itor = hostMapUUIDList.keySet().iterator();
			while (itor.hasNext()){
				String thisHost = itor.next();
				ArrayList<String> thisList = hostMapUUIDList.get(thisHost);
				System.out.println(thisHost + "," + thisList.size());
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public static void generateOwnerTag(File dirFile, File tagFile){
		File[] files = dirFile.listFiles();
		try {
			FileOutputStream fos = new FileOutputStream(tagFile);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			BufferedWriter bw = new BufferedWriter(osw);
			
			for (int i=0; i<files.length; ++i){
				File thisFile = files[i];
				bw.write("#");
				bw.write(thisFile.getName());
				bw.write("\n");
				WSDLFileAnalyzer wsdlFileAnalyzer = new WSDLFileAnalyzer();
				try {
					WSDLFile wFile = wsdlFileAnalyzer.analyzeWSDL(thisFile.getAbsolutePath());
					
					ArrayList<Endpoint> endpointList = wFile.endPoints;
					if (endpointList != null){
						HashSet<String> hostURL = new HashSet<String>();
						for (Endpoint endpoint : endpointList) {
							try{
								URL locationURL = new URL(endpoint.getLocation());
								String thisHost = locationURL.getHost();
								if (thisHost.startsWith("127.0.0.1") || 
										thisHost.startsWith("localhost")){
									continue;
								}
								int count = StringDealer.countTokens(thisHost, ".");
								if (count > 2){
									thisHost = new String(thisHost.substring(thisHost.indexOf(".") + 1));
								}
								hostURL.add(thisHost);
							}catch (Exception e) {
								// TODO: handle exception
							}
						}
						for (String urlString : hostURL) {
							bw.write(urlString);
							bw.write("\n");
						}
					}
					System.out.println(i + ",ok");
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			bw.close();
			osw.close();
			fos.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
